package org.example.chat.utils;

import org.example.chat.config.ServerConfigProperties;
import org.example.chat.security.entity.LoginUser;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class BaseUtils {
    @Autowired
    protected IDGenerate idGenerate;
    @Autowired
    protected ThreadPoolTaskExecutor executor;
    @Autowired
    public StringRedisTemplate redisTemplate;
    @Autowired
    protected RedissonClient redissonClient;
    @Autowired
    protected ServerConfigProperties.CacheProperties cacheProperties;
    @Autowired
    protected CommonAlgorithm commonAlgorithm;


    /**
     * 获取当前登录用户信息
     * @return
     */
    public LoginUser getLoginUserInfo() {
        return  CommonUtils.getLoginUserInfo();
    }

    /**
     * 根据请求上下文处理器获取请求对象
     * @return
     */
    public HttpServletRequest getHttpServletRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        return request;
    }

    /**
     * 根据请求上下文处理器获取响应对象
     * @return
     */
    public HttpServletResponse getHttpServletResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        return response;
    }

    public String getClientIp() {
        HttpServletRequest request = getHttpServletRequest();
        // 1. 首先假设客户端是通过代理服务器访问的服务端，所以从请求头X-Forwarded-For中获取
        // client, proxy1, proxy2, ...  注： client是客户端真实ip, proxy$是中间代理服务器的ip, 我们这里把所有的都记录了， 如果值记录client,截取即可
        String clientIp = request.getHeader("X-Forwarded-For");
        // 2. 如果从请求头X-Forwarded-For中获取不到, 则根据Proxy-Client-IP和WL-Proxy-Client-IP请求头获取
        // 在Linux加Squid的组合做为代理服务器的网络环境中，若Squid的配置forwarded_for 设成了 off则：X-Forwarded-For： unknown。此时在apache+WebLogic的系统中Proxy-Client-IP和WL-Proxy-Client-IP可以反应出用户的真实IP
        if (clientIp == null || clientIp.length() == 0 || "unknown".equals(clientIp)) {
            clientIp = request.getHeader("Proxy-Client-IP");
        }
        if (clientIp == null || clientIp.length() == 0 || "unknown".equals(clientIp)) {
            clientIp = request.getHeader("WL-Proxy-Client-IP");
        }
        // 3. 如果根据Proxy-Client-IP和WL-Proxy-Client-IP请求头获取不到， 则说明没有通过代理访问，则直接通过request获取
        if (clientIp == null || clientIp.length() == 0 || "unknown".equals(clientIp)) {
            clientIp = request.getRemoteAddr();
        }
        return clientIp;
    }
}
